#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include "strings.h"
#include "parsing.h"



void test_split() 
{
  char test_string[100] = "asdas  ; dasd asda; das das d=3";
  char * * tokens = init_tokens(10, 100);
  split(test_string, ';', tokens);
  assert(strcmp(tokens[0], "asdas  ") == 0);
  assert(strcmp(tokens[1], " dasd asda") == 0);
  assert(strcmp(tokens[2], " das das d=3") == 0);
  assert(tokens[3] == 0);
  free(tokens[0]);
}


void test_content_disposition()
{
  char value[100] = "form-data; name=\"paper\"; filename=\"Firefox_wallpaper.png\"";
  struct fileupload_http_header_t  header;
  parse_content_disposition(value, &header); 
  assert(strcmp(header.name, "paper") == 0);
  assert(strcmp(header.file_name, "Firefox_wallpaper.png") == 0);
}


void test_header_line()
{
  char line[100] = "Content-Disposition: form-data; name=\"paper\"; filename=\"Firefox_wallpaper.png\"";
  struct fileupload_http_header_t  header;
  http_header_parse_line(line, &header);
  assert(strcmp(header.name, "paper") == 0);
  assert(strcmp(header.file_name, "Firefox_wallpaper.png") == 0);
}

void test_header()
{
  char line[100] = "aaaa\nContent-Disposition: form-data; name=\"paper\"; filename=\"Firefox_wallpaper.png\"\n\n";
  struct fileupload_http_header_t  header;
  http_header_parse(&header);
  printf(header.name);
  printf("=== Header after all ===<br/>\n");
  http_header_dump(&header);
  assert(strcmp(header.name, "paper") == 0);
  assert(strcmp(header.file_name, "Firefox_wallpaper.png") == 0);

}

void test_suffix()
{
  assert(is_suffix(".pdf", 'p', 'd', 'f'));
  assert(!is_suffix(".doc", 'p', 'd', 'f'));
  assert(!is_suffix("", 'p', 'd', 'a'));
  assert(is_suffix(".doc", 'd', 'o', 'c'));
  assert(is_suffix("asdasd.doc", 'd', 'o', 'c'));
}

void test_mime()
{
  char buffer[100];
  get_mime_type_by_filename("ala.pdf", buffer);
  assert(strcmp(buffer, "application/pdf" ) == 0);
  get_mime_type_by_filename("", buffer);
  assert(strcmp(buffer, "application/bin" ) == 0);
  get_mime_type_by_filename("ala.doc", buffer);
  assert(strcmp(buffer, "application/msword" ) == 0);
}

int main()
{
  test_split();
  test_content_disposition();
  test_header_line();
//  test_header();
  test_suffix();
  test_mime();
  return 0;
}

